import {
  getParamsTemplate
} from "./condition_util/ConditionUtil";
import {
  getPairsKeyValues,
  KeyValuePairs,
  SqlDataType
} from "./SqlUtil";


/**
 * INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
 * @param tableName
 * @param keyValuePairs
 * @returns
 */
export function generateInsertSqlAndArgs(tableName: string, pairs: KeyValuePairs): [string, SqlDataType[]] {
  const sqlHeader: string = `INSERT INTO ${tableName}`;
  const [keys, args] = getPairsKeyValues(pairs);
  const _fieldsString = keys.join(" , ");
  const fieldsStr = `( ${_fieldsString} )`;
  const _templateStr = getParamsTemplate(keys.length);
  const templateStr = `( ${_templateStr} )`;
  const sql = `${sqlHeader} ${fieldsStr} VALUES ${templateStr}`
  return [sql, args];
}